package com.chapter1.basic.stack;

/** 
 * @ClassName: FixCapacityStack 
 * @Description: 自定义固定容量栈
 * @author minjun minjun@bw30.com
 * @date 2015-2-11 下午10:37:18 
 *  
 */
public class FixCapacityStack<Item> {
	
	private Item[] items;
	
	private int capacity=0;
	
	private int index=0;
	
	@SuppressWarnings("unchecked")
	public FixCapacityStack(int capacity) {
		this.capacity=capacity;
		items=(Item[]) new Object[capacity];
	}

	public void push(Item item){
		if(index>capacity-1){
			throw new RuntimeException("无法再添加，已达到容器上限");
		}else{
			items[index++]=item;
		}
	}
	
	public Item pop(){
		if(index==0){
			return null;
		}else{
			return items[--index];
		}
	}
	
}
